package com.eckey.pub.aop;

import com.alibaba.fastjson.JSON;
import com.eckey.pub.annotation.AllowList;
import com.eckey.pub.result.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

/**
 * @Author: ChengLiang
 * @CreateTime: 2023-05-22  11:27
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Aspect
@Component
public class AllowListAspect {

    @Resource
    private String allowListConfig;

    @Pointcut("@annotation(com.eckey.pub.annotation.AllowList)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Method method = null;
        try {
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            method = proceedingJoinPoint.getTarget().getClass().getMethod(signature.getName(), signature.getParameterTypes());
        } catch (Exception e) {
            log.error("Method Trans Error:{}", e);
        }
        AllowList annotation = method.getAnnotation(AllowList.class);
        String keyWord = annotation.keyWord();
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        //获取参数
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        assert sra != null;
        HttpServletRequest request = sra.getRequest();
        String paramsValue = getParamsValue(keyWord, request);

        String[] split = allowListConfig.split(",");
        List<String> list = Arrays.asList(split);
        //白名单它通过
        if (list.contains(paramsValue)) {
            return proceedingJoinPoint.proceed();
        }
        String result = annotation.result();
        if (!StringUtils.isEmpty(result)) {
            ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
            return JSON.toJSONString(resultBean);
        }
        //返回ResultBean对象
        return JSON.toJSONString(new ResultBean<>(ResultBean.ErrorMessage.NOAUTH, ""));
    }

    public String getParamsValue(String keyWord, HttpServletRequest request) {
        String paramValue = null;
        Enumeration parameter = request.getParameterNames();
        while (parameter.hasMoreElements()) {
            String param = (String) parameter.nextElement();
            if (keyWord.equals(param)) {
                paramValue = request.getParameter(param);
                break;
            }
        }
        return paramValue;
    }

}
